package cn.tedu.charging.order.producer;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;
    public boolean sendMessage(String topic, String message){
        log.debug("向主题:{},发送消息:{}",topic,message);
        try {
            mqttClient.publish(topic,message.getBytes(StandardCharsets.UTF_8),0,false);
            return true;
        } catch (MqttException e) {
            log.error("向主题:{},发送消息:{}失败",topic,message,e);
            return false;
        }
    }
}
